package com.spzx.product.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
public class ThreadPoolConfig {
    @Bean
    public ThreadPoolExecutor threadPoolTaskExecutor(){
        int coreSize = Runtime.getRuntime().availableProcessors();
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                coreSize*2
                ,coreSize*2
                ,0
                , TimeUnit.SECONDS
                ,new ArrayBlockingQueue<>(300)
                , Executors.defaultThreadFactory()
                //CallerRunsPolicy 超过最大线程交给主线程执行
                ,new ThreadPoolExecutor.CallerRunsPolicy());
        executor.prestartCoreThread();
        return executor;
    }
}
